package com.baylor.server.business.util;


import java.util.*;

/**
 * Created by IntelliJ IDEA. User: zhaoxu Date: 12-3-12 Time: 下午5:45 To change
 * this template use File | Settings | File Templates.
 */
public class Maps {
	public static <K, V> Map<K, V> newHashMap() {
		return new HashMap<K, V>();
	}

	public static <K, V> Map<K, V> unmodifiableMap(Map<K, V> source) {
		return Collections.unmodifiableMap(source);
	}

	public static Map<String, String> convertMap(Map<String, String[]> source) {
		Map<String, String> retMap = newHashMap();
		for (Map.Entry<String, String[]> entry : source.entrySet()) {
			String[] values = entry.getValue();
			String value = "";
			if (values.length == 1) {
				value = values[0];
			} else {
				StringBuilder sb = new StringBuilder();
				for (String v : values) {
					sb.append(';').append(v);
				}
				if (sb.length() > 0) {
					value = sb.toString().substring(1);
				}
			}
			retMap.put(entry.getKey(), value);
		}
		return retMap;
	}

	public static String mapToJson(Map<String, String> map) {
		if (map == null || map.size() < 1) {
			return null;
		}
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		for (Iterator<String> iterator = map.keySet().iterator(); iterator
				.hasNext();) {
			String key = (String) iterator.next();
			buffer.append(key);
			buffer.append("\":\"");
			buffer.append(map.get(key));
			buffer.append("\"");
			if (iterator.hasNext()) {
				buffer.append(",\"");

			}
		}
		buffer.append("}");
		return buffer.toString();
	}
	
	

	/**
	 * 备用
	 * @author hyxue
	 * @param list
	 * @param column
	 * @return
	 */
	public static List<Map<String, Object>> sortData1(List<Map<String, Object>> list,final String column) {  
	    System.out.println("对WCM数据进行排序");  
	    if (list.size() !=0) {  
	        Comparator<Map<String, Object>> mapComprator = new Comparator<Map<String, Object>>() {  
	            @Override  
	            public int compare(Map<String, Object> o1,  
	                    Map<String, Object> o2) {  
	                // do compare.  
	                if (Integer.valueOf(o1.get(column).toString()) < Integer  
	                        .valueOf(o2.get(column).toString())) {  
	                    return 1;  
	                } else {  
	                    return -1;  
	                }  
	            }  
	        };  
	        Collections.sort(list, mapComprator);  
	    } else {  
	        new Exception("排序没有取到数据");  
	    }  
	    return list;  
	} 

}
